﻿
using AuthCenter.CSM.Host;
using AuthCenter.Data.Database;
using ExcelConfig;
using System;
using System.Text;
using System.Threading.Tasks;

namespace AuthCenter
{
    static class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            if (args.Length == 1 && args[0] == "cmd")
            {
                do
                {
                    Console.Write("# ");
                    var mode = Console.ReadLine();
                    if (mode == null) goto END;
                    switch (mode.Trim().ToLower()) {
                        case "exit": goto END;
                        case "app":
                            Console.Write("APP: ");
                            var app_code = Console.ReadLine();
                            Console.Write("Name: ");
                            var app_name = Console.ReadLine();
                            Console.Write("Passway(md6): ");
                            var pass_way = Console.ReadLine();
                            Console.Write("Scheme(Bearer): ");
                            var scheme = Console.ReadLine();
                            if (app_code == null || app_name == null) continue;
                            if (pass_way == null) pass_way = "md6";
                            if (scheme == null) scheme = "Bearer";
                            var outpass = string.Empty;
                            using (var db = new AuthDB())
                            {
                                outpass = db.MakeApp(app_code, app_name, pass_way, scheme);
                            }
                            Console.WriteLine($"OK&Password: {outpass}");
                            break;
                        case "user":
                            Console.Write("Account: ");
                            var account = Console.ReadLine();
                            Console.Write("Password: ");
                            var password = Console.ReadLine();
                            Console.Write("Passway(B1): ");
                            var user_passway = Console.ReadLine();
                            if (user_passway == null) user_passway = "B1";
                            if (account == null || password == null) continue;
                            Guid userId;
                            using (var db = new AuthDB())
                            {
                                userId = db.MakeUser(account, password, user_passway);
                            }
                            Console.WriteLine($"OK: {userId}");
                            break;
                        case "bind":
                            Console.Write("Account: ");
                            var bind_account = Console.ReadLine();
                            Console.Write("App: ");
                            var bind_app = Console.ReadLine();
                            if (bind_account == null || bind_app == null) continue;
                            using (var db = new AuthDB())
                            {
                                Guid bind_userId = db.GetUserID(bind_account);
                                db.LinkApp(bind_userId, bind_app);
                            }
                            Console.WriteLine($"OK");
                            break;
                        case "localhost":
                            Console.Write("Localhost Addr(y/n): ");
                            var localhost = Console.ReadLine();
                            var islocalhost = localhost != null && localhost.ToLower() == "y";
                            AppConfig.WriteYesOrNo("本地回环", islocalhost);
                            Console.WriteLine($"OK");
                            break;
                        case "listen":
                            Console.Write("Listen Addr: ");
                            var listen_addr = Console.ReadLine();
                            AppConfig.WriteText("监听地址", listen_addr);
                            Console.WriteLine($"OK");
                            break;
                        case "cors":
                            Console.Write("Cors: ");
                            var cors_uri = Console.ReadLine();
                            AppConfig.WriteText("跨域目标", cors_uri);
                            AppConfig.WriteText("跨域请求", "支持");
                            Console.WriteLine($"OK");
                            break;
                        case "db-device":
                            Console.Write("DB(device): ");
                            var db_device = Console.ReadLine();
                            AppConfig.WriteText("auth_device", db_device);
                            Console.WriteLine($"OK");
                            break;
                        case "db-center":
                            Console.Write("DB(center): ");
                            var db_center= Console.ReadLine();
                            AppConfig.WriteText("auth_center", db_center);
                            Console.WriteLine($"OK");
                            break;
                        case "db-log":
                            Console.Write("DB(log): ");
                            var db_log = Console.ReadLine();
                            AppConfig.WriteText("auth_log", db_log);
                            Console.WriteLine($"OK");
                            break;
                        case "expire":
                            Console.Write("Expire(s): ");
                            var expire = Console.ReadLine();
                            AppConfig.WriteText("登录有效期", expire);
                            Console.WriteLine($"OK");
                            break;
                        case "text-ecode":
                            Console.Write("Text Encrypt Code: ");
                            var text_ecode = Console.ReadLine();
                            AppConfig.WriteText("敏感字段密钥", text_ecode);
                            Console.WriteLine($"OK");
                            break;
                        case "watermark":
                            Console.Write("Watermark Rule: ");
                            var watermark_rule = Console.ReadLine();
                            AppConfig.WriteText("水印规则", watermark_rule);
                            Console.WriteLine($"OK");
                            break;
                        case "help":
                            Console.WriteLine("exit");
                            Console.WriteLine("app");
                            Console.WriteLine("user");
                            Console.WriteLine("bind");
                            Console.WriteLine("localhost");
                            Console.WriteLine("listen");
                            Console.WriteLine("cors");
                            Console.WriteLine("db-device");
                            Console.WriteLine("db-center");
                            Console.WriteLine("db-log");
                            Console.WriteLine("expire");
                            Console.WriteLine("text-ecode");
                            Console.WriteLine("watermark");
                            break;
                    }

                } while (true);
            }
            else
            {
                try
                {
                    IWebHost host = new WebHost();
                    host.Start().Wait();
                    while (host.State == TouchSocket.Sockets.ServerState.Running) Task.Delay(1000).Wait();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }
            }
            END:
            Console.WriteLine("End");
        }
    }
}